package com.primera.geneiousplugin.python;

import java.io.File;
import java.io.FilenameFilter;

import com.biomatters.geneious.publicapi.plugin.Options;

/**
 * The configuration options presented to the user before they run the plugin.
 * @author rich oconnell
 */
public class PythonOptions extends Options {
	static final String MSG_PYCMD = "Must specify the command used to run Python";
	static final String MSG_SCRIPT = "The location of a Python script to execute must be given.";
	static final String P_ARGS = "scriptArgs";
	static final String P_SCRIPT_LOC = "pyFile";
	static final String P_PYTHON_CMD = "pyCommand";
	static final String P_NEW_FOLDER = "pyNewFolder";

	public PythonOptions() {
		super(PythonOptions.class);
		init();
	}
	/**
	 * Create the options pane.
	 */
	private void init() {
		addStringOption(P_PYTHON_CMD, "Python command:", "python")
			.setFillHorizontalSpace(false);
		addFileSelectionOption(P_SCRIPT_LOC, "Python script location:",
				"", new String[]{}, "Browse",
				new FilenameFilter() {
					public boolean accept(File dir, String name) {
						return name.endsWith("py");
					}
				}
		).setFillHorizontalSpace(false);
		addStringOption(P_ARGS, "Arguments:", "").setFillHorizontalSpace(false);
		addStringOption(P_NEW_FOLDER, "Result Folder:", "results-" + (int)(Math.random() * 1000))
			.setFillHorizontalSpace(false);
	}
	/**
	 * {@inheritDoc}
	 */
	// validate
	@Override
	public String verifyOptionsAreValid() {
		String pyCmd = getPythonCommand();
		if (pyCmd == null || pyCmd.trim().length() == 0) {
			return MSG_PYCMD;
		}
		String fname = getScriptLocation();
		if (fname == null) {
			return MSG_SCRIPT;
		}
		File file = new File(fname);
		return file.exists() ? null : MSG_SCRIPT;
	}

	public String getPythonCommand() {
		return (String)getValue(P_PYTHON_CMD);
	}
	public String getScriptLocation() {
		return (String)getValue(P_SCRIPT_LOC);
	}
	public String getArguments() {
		return (String)getValue(P_ARGS);
	}
	public String getNewFolder() {
		return (String)getValue(P_NEW_FOLDER);
	}
	public String debug() {
		return getPythonCommand() + " " + getScriptLocation() + " " + getArguments();
	}
}